<!DOCTYPE html>
<html>
<head>
    <title>项目分配2021</title>
    <link rel="stylesheet" href="/static/plugin/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/plugin/zTree_v3/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="/static/plugin/zTree_v3/css/demo.css">
    <script src="/static/plugin/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="/static/plugin/layui/layui.js"></script>
    <script src="/static/plugin/ztree/js/jquery.ztree.core.js"></script>
    <script src="/static/plugin/ztree/js/jquery.ztree.excheck.js"></script>
    <script src="/static/plugin/ztree/js/jquery.ztree.exedit.js"></script>
    <script type="text/javascript" src="/static/js/base.js"></script>
    <style>
        .layui-table th{
            font-weight: bold;
            font-size: 16px;
        }
        .layui-table td{
            font-weight: bold;
            font-size: 16px;
        }
    </style>
<body class="layui-anim layui-anim-up">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a>
          <cite>项目分配</cite>
        </a>
      </span>
</div>
<div class="x-body">

    <div class="layui-row" style="margin-bottom: 18px">
        <div class="layui-form">
            <div class="layui-input-inline">

                <div class="layui-input-inline" style="width: 150px">
                    <input class="layui-input" id="keyword01" placeholder="请输入项目名称" style="width: 150px">
                </div>
                <button class="layui-btn layui-btn-normal" data-type="reload"><i class="layui-icon layui-icon-search"></i>检索</button>

            </div>
        </div>
    </div>

    <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
    <p style="border-style: solid;"></p>
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item" id="checkboxExpert">
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" id="subBtn" lay-submit lay-filter="addForm">提交</button>
                    <button  class="layui-btn layui-btn-primary">取消</button>
                </div>
            </div>
        </form>
    </div>


</div>


<script>


    //专家
    $(document).ready(function () {
        $('#checkboxExpert').empty();
        layui.use(['form'], function(){
            var form=layui.form;

            $.ajax({
                type:'get',
                url:'/projectExpert/queryList',
                success:function (result) {
                    for (let i = 0; i < result.length; i++) {
                        let dom = '<input type="checkbox" lay-skin="expert" name="expert" value="'+result[i].userId+'" title="'+result[i].realName+"  "+result[i].email+" "+ result[i].remarkTwo+'项">'
                        $('#checkboxExpert').append(dom);
                    }

                    form.render();
                }
            });
        })

    })

    layui.use(['table','layer','upload','form','laydate'], function(){
        var table = layui.table;
        var form=layui.form;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#date1',
            value:new Date()
        });
        laydate.render({
            elem: '#date2'
        });


        laydate.render({
            elem: '#date3',
            type: 'month',
            value: new Date(),
            done:function(value, date, endDate) {
                $('#dateTime').val(value);
            }
        });

        var $ = layui.jquery, active = {
            reload:function () {
                table.reload('contentTable',{
                    method:'get'
                    ,where:{
                        'projectName':$('#keyword01').val()
                    }
                    ,page: {
                        curr: 1//重新从第 1 页开始
                    }
                });
            }
        }

        table.render({
            elem: '#tableList'
            ,id:'contentTable'
            ,url: '/projectExpert/queryPage'
            ,toolbar: '#toolbar'
            ,cellMinWidth: 80
            ,height: 'full-200'
            ,where:{
                'projectName':""
            }
            , cols: [

                [
                    {type:'radio'},
                    {field:'uid', title:'序号', width: 60, type:'numbers',align:'center'}
                    , {field: 'pname', title: '项目名称', sort: true}
                    , {field: 'pnumber', title: '项目序号', sort: true,width: 120}
                    , {field: 'type', title: '类型',width: 120}
                    , {field: 'completeCompany', title: '单位',width: 300}
                    , {field: 'expert', title: '分配专家'}
                ]
            ]
            /* , page: true*/
            /*,totalRow: true*/
            ,done : function(res, curr, count){
                $('th').css({
                    'background-color': '#3388CB', 'color': '#fff','font-weight':'bold',
                })
            }
        });

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.layui-col-md12 .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.select .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听表格复选框选择
        table.on('radio(tableList)', function(obj){
            let data = obj.data;
            $('#checkboxExpert').empty()
            $.ajax({
                type:'get',
                data:{projectId:data.id},
                url:'/projectExpert/queryListByProjectId',
                success:function (result) {
                    for (let i = 0; i < result.length; i++) {
                        let dom = "";
                        if (result[i].remarkOne == 0) {
                            dom = '<input type="checkbox" checked  lay-skin="expert" name="expert" value="'+result[i].userId+'" title="'+result[i].realName+"  "+result[i].email+" "+ result[i].remarkTwo+'项">'
                        }else {
                            dom = '<input type="checkbox"  lay-skin="expert" name="expert" value="'+result[i].userId+'" title="'+result[i].realName+"  "+result[i].email+" "+ result[i].remarkTwo+'项">'
                        }
                        $('#checkboxExpert').append(dom);
                    }
                    form.render();
                }
            });

        });


        /**  data:{itemName:"12",startDate:'2019-09-20'},
         * 通用表单提交(AJAX方式),totalMoney:100,startDate:'2019-09-20',dueDate:'2019-09-20'
         */
        form.on('submit(addForm)', function () {
            $("#subBtn").attr("disabled",true);
            //项目
            let checkStatus = table.checkStatus("contentTable");
            if (checkStatus.data.length === 0) {
                layer.msg("请选择项目！", {icon: 2});
                setTimeout(function(){
                    location.reload();
                },500);
                return true;
            }

            if (checkStatus.data.length !== 1){
                layer.msg("请选择一个项目！", {icon: 2});
                setTimeout(function(){
                    location.reload();
                },500);
                return true;
            }


            //将页面全部复选框选中的值拼接到一个数组中
            let arr_box = [];
            $('input[type=checkbox]:checked').each(function() {
                arr_box.push($(this).val());
            });
            console.log(arr_box);

            if (arr_box.length === 0) {
                layer.msg("请选择专家！", {icon: 2});
                setTimeout(function(){
                    location.reload();
                },500);
                return true;
            }

            $.ajax({
                type:'post',
                url:'/projectExpert/add',
                data:{userId:arr_box.toString(),projectId:checkStatus.data[0].id},
                cache:false,
            }).done(
                function(res) {
                    if (res) {
                        layer.msg('新增成功',{icon:6})
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }
                }
            ).fail(
                function() {
                    layer.msg('新增失败',{icon:5})
                    setTimeout(function(){
                        location.reload();
                    },500);
                }
            )
            return false;
        })

    });
</script>
</body>

</html>
